package com.liqi.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.liqi.common.utils.StringUtils;
import com.liqi.sms.domain.MessageForWarn;
import com.liqi.sms.service.ISendMessageService;
import com.liqi.sms.utils.CenterApiEnum;
import com.liqi.sms.utils.MessageSendBusiness;
import com.liqi.sms.utils.PlatformConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendMessageServiceImpl implements ISendMessageService {

    private final static Logger logger = LoggerFactory.getLogger(SendMessageServiceImpl.class);

    @Autowired
    PlatformConfig platformConfig;

    @Override
    public void sendMessage(MessageForWarn messageForWarn) {
        logger.info ( "----------------投放设备故障信息短信推送----------------" );
        JSONObject reqContent = new JSONObject();
        reqContent.put("mobiles", messageForWarn.getOperationPhoneNum());// 手机号码（接收短信的手机号码多个 号码用半角逗号分隔）
        String content = "";//短信内容
        String message = "";

        if(StringUtils.isNotBlank(messageForWarn.getOperationPhoneNum())) {

            if("人工上报".equals(messageForWarn.getReportedType())) {
                content = "你好，" + messageForWarn.getOperationUserName() + "\r\n"
                        + "你负责运维的设备于" + messageForWarn.getErrorTime() + "由人工上报异常，请尽快查看处理。" + "\r\n"
                        + "设备Id：" + messageForWarn.getDeviceId() + "\r\n"
                        + "设备投放名称:" + messageForWarn.getDeploymentName() + "\r\n"
                        + "设备位置:" + messageForWarn.getDeploymentAddress() + "\r\n"
                        + "异常信息:" + messageForWarn.getErrorMessage() + "\r\n"
                        + "上报类型:" + messageForWarn.getReportedType() + "\r\n";
            }
            else {
                content = "你好，" + messageForWarn.getOperationUserName() + "\r\n"
                        + "你负责运维的设备于:" + messageForWarn.getErrorTime() + "由心跳上报异常，请尽快查看处理。" + "\r\n"
                        + "设备Id:" + messageForWarn.getDeviceId() + "\r\n"
                        + "设备投放名称:" + messageForWarn.getDeploymentName() + "\r\n"
                        + "设备位置:" + messageForWarn.getDeploymentAddress() + "\r\n"
                        + "异常信息:" + messageForWarn.getErrorMessage() + "\r\n"
                        + "上报类型:" + messageForWarn.getReportedType() + "\r\n";
            }

            reqContent.put("content", content);// 短信内容
            logger.info("政务取票信息短信推送信息{}", JSON.toJSONString(reqContent, SerializerFeature.WriteMapNullValue));
        }

        try{
            if(StringUtils.isNotBlank(messageForWarn.getOperationPhoneNum())) {
                message = MessageSendBusiness.getInstance().shanWeiSendPost("util/authenticationLogin/message",reqContent.toJSONString() , platformConfig);
                logger.info("短信发送成功: [{}]", message);
            }else {
                logger.info("手机号为空，不发短信");
            }
        }catch (Exception e){
            logger.info("短信发送异常", e);
        }
    }

}
